package com.first.nell.components.dialog

import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.databinding.DialogFirstNellAddWalletMobileFcfaBinding
import com.first.nell.extend.getFirstNellSafeFirstNellTag
import com.first.nell.extend.recordFirstNellClickFirstNellEvent
import com.first.nell.utils.AppConfigUtil

class FcfaAddWalletDialog : DialogFragment() {

    lateinit var mFirstNellViewFirstNellBinding: DialogFirstNellAddWalletMobileFcfaBinding

    private var mFirstNellOnFirstNellActionFirstNellListener: (Int, String, String, String) -> Unit =
        { type, mobile, sms, channel -> }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mFirstNellViewFirstNellBinding =
            DialogFirstNellAddWalletMobileFcfaBinding.inflate(layoutInflater)
        return mFirstNellViewFirstNellBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFirstNellSubFirstNellViews()
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.cleanFirstNell()
        mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.cleanFirstNell()
        mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.cleanFirstNell()

        val channels = AppConfigUtil.getFirstNellWalletFirstNellChannelsFirstNellFcfa()
        mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.setFirstNellDataFirstNellList(
            channels
        )

        updateFirstNellCommitFirstNellStateFirstNellUI()
    }

    private fun initFirstNellSubFirstNellViews() {
        mFirstNellViewFirstNellBinding.submitDialogAddWalletMobileFCFACancel.setOnClickListener {
            try {
                val checkFirstNellEmptryFirstNellOptions =
                    isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted()
                mFirstNellViewFirstNellBinding.submitDialogAddWalletMobileFCFACancel.recordFirstNellClickFirstNellEvent(
                    remark = checkFirstNellEmptryFirstNellOptions
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
            dismiss()
        }
        mFirstNellViewFirstNellBinding.submitDialogAddWalletMobileFCFAConfirm.setOnClickListener {
            val checkFirstNellEmptryFirstNellOptions =
                isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted()

            mFirstNellViewFirstNellBinding.submitDialogAddWalletMobileFCFAConfirm.recordFirstNellClickFirstNellEvent(
                checkFirstNellEmptryFirstNellOptions
            )

            val mobile =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.getFirstNellContent()
            val sms =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getFirstNellContent()
            val type =
                mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getFirstNellContentFirstNellValue()
            if (checkFirstNellEmptryFirstNellOptions.isBlank()) {
                mFirstNellOnFirstNellActionFirstNellListener.invoke(
                    2,
                    mobile,
                    sms,
                    type
                )
            } else {
                Toast.makeText(
                    context,
                    "Veuillez remplir les champs obligatoires",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
        mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.setFirstNellOnFirstNellListFirstNellItemFirstNellClickFirstNellListener { s, s2 ->
            //更新提交按键状态
            updateFirstNellCommitFirstNellStateFirstNellUI()
        }

        mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.setFirstNellOnFirstNellTextFirstNellChangedFirstNellListener { content ->
            //更新提交按键状态
            updateFirstNellCommitFirstNellStateFirstNellUI()
        }
        mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.setFirstNellOnFirstNellTextFirstNellChangedFirstNellListener { content ->
            //更新提交按键状态
            updateFirstNellCommitFirstNellStateFirstNellUI()
        }
    }

    fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.isFirstNellFormFirstNellCompleted()) {
            val gTag =
                mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.isFirstNellCompleted()) {
            val gTag =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.isFirstNellCompleted()) {
            val gTag =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

    fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompletedFirstNellSlient(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.isFirstNellFormFirstNellCompletedFirstNellSilent()) {
            val gTag =
                mFirstNellViewFirstNellBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.isFirstNellCompletedFirstNellSlient()) {
            val gTag =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumber.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.isFirstNellCompletedFirstNellSlient()) {
            val gTag =
                mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

    private fun updateFirstNellCommitFirstNellStateFirstNellUI() {
//        val checkFirstNellEmptryFirstNellOptions =
//            isFirstNellAllFirstNellFormFirstNellDataFirstNellCompletedFirstNellSlient()
//        if (checkFirstNellEmptryFirstNellOptions.isBlank()) {
//            mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.setFirstNellStateFirstNellBan(false)
//        } else {
//            mFirstNellViewFirstNellBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.setFirstNellStateFirstNellBan(true)
//        }
    }

    fun showFirstNellDialogFirstNellNow(
        fragmentFirstNellManager: FragmentManager,
        clickFirstNellListener: (Int, String, String, String) -> Unit = { type, mobile, sms, channel -> }
    ) {
        mFirstNellOnFirstNellActionFirstNellListener = clickFirstNellListener
        showNow(
            fragmentFirstNellManager,
            FcfaAddWalletDialog::class.java.simpleName
        )
    }
}